local sk_le__yiji = fk.CreateSkill {

  name = "sk_le__yiji",

  tags = {},

}

Fk:loadTranslationTable {
  ["sk_le__yiji"] = "遗计",
  [":sk_le__yiji"] = "回合结束阶段，或当你受到1点伤害后，你可以观看牌堆顶的四张牌或手牌最多角色的手牌，然后可以将其中至多三张牌交给任意角色。",
  ["$sk_le__yiji1"] = "意之所随，不可言传。",
  ["$sk_le__yiji2"] = "隔岸观火，坐立天下。",
  ["#sk_le__yiji-choice_invoke"] = "遗计：请选择观看牌堆顶的四张牌或手牌最多角色的手牌",
  ["#sk_le__yiji-choice_players"] = "遗计：请选择一名角色",
  ["#sk_le__yiji-choice_cards"] = "遗计：请选择至多3张牌"
}

local sk_le__yiji_Effect = {
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, { skill_name = sk_le__yiji.name }) then
      local choice = room:askToChoice(player, {
        choices = { "角色", "牌堆顶", "Cancel" },
        skill_name = sk_le__yiji.name,
        prompt = "#sk_le__yiji-choice_invoke",
        cancelable = true
      })
      if choice == "" or choice == "Cancel" then
        return false
      else
        event:setCostData(self, choice)
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = event:getCostData(self)
    local cards
    if choice == "角色" then
      local targets = table.filter(room.alive_players, function(p)
        return table.every(room.alive_players, function(p2)
          return p:getHandcardNum() >= p2:getHandcardNum()
        end)
      end)
      if #targets == 0 or targets[1]:getHandcardNum() == 0 then return end
      local target_p = room:askToChoosePlayers(player, {
        targets = targets,
        max_num = 1,
        min_num = 1,
        skill_name = sk_le__yiji.name,
        prompt = "#sk_le__yiji-choice_players",
        cancelable = true
      })
      if #target_p == 0 then return end
      cards = target_p[1]:getCardIds("h")
    else
      cards = room:getNCards(4)
    end
    local choices = { "确定" }
    local cards, _ = room:askToChooseCardsAndChoice(player, {
      prompt = "#sk_le__yiji-choice_cards",
      cards = cards,
      skill_name = sk_le__yiji.name,
      min_num = 1,
      max_num = 3,
      cancel_choices = { "Cancel" },
      choices = choices,
    })
    if #cards > 0 then
      local target_p = room:askToChoosePlayers(player, {
        targets = room.alive_players,
        max_num = 1,
        min_num = 1,
        skill_name = sk_le__yiji.name,
        prompt = "#sk_le__yiji-choice_players",
        cancelable = false
      })
      room:obtainCard(target_p[1], cards, false, fk.ReasonPrey, player, sk_le__yiji.name)
    end
  end,
}

sk_le__yiji:addEffect(fk.TurnEnd, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk_le__yiji.name)
  end,
  on_cost = sk_le__yiji_Effect.on_cost,
  on_use = sk_le__yiji_Effect.on_use
})

sk_le__yiji:addEffect(fk.Damaged, {
  anim_type = "control",
  trigger_times = function(self, event, target, player, data)
    return data.damage
  end,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk_le__yiji.name)
  end,
  on_cost = sk_le__yiji_Effect.on_cost,
  on_use = sk_le__yiji_Effect.on_use
})



return sk_le__yiji
